package com.sc.nft.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.admin.request.OperateDaoWalletRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.UserCreatorInfoDao;
import com.sc.nft.entity.UserCreatorInfo;
import com.sc.nft.enums.SendSMSEnum;
import com.sc.nft.enums.wallet.DaoWalletTypeEnum;
import com.sc.nft.service.CheckParameterService;
import com.sc.nft.service.DaoWalletService;
import com.sc.nft.service.StaticDataService;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/dao/wallet")
@Api(tags = "企业钱包")
public class DaoWalletAdminController {
    private final StaticDataService staticDataService;
    private final CheckParameterService checkParameterService;
    private final DaoWalletService daoWalletService;
    private final UserCreatorInfoDao userCreatorInfoDao;

    @PostMapping(value = "/operateDaoWallet", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "操作企业钱包余额", notes = "操作企业钱包余额", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result operateDaoWallet(@Valid @RequestBody OperateDaoWalletRequest request) {
        String noticePhone = staticDataService.getByType("notice_phone").getValue();
        checkParameterService.CheckVerificationcode(noticePhone,
                request.getSmsId(),
                request.getCodes(),
                SendSMSEnum.OPERATING_BALANCE.getCode());
        UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(request.getDaoId());
        if (ObjectUtil.isNull(userCreatorInfo)) {
            return Result.fail("未找到该dao组织");
        }
        if (request.getIsType()) {
            daoWalletService.addBalance(userCreatorInfo.getId(), null, request.getAmount(), BigDecimal.ZERO, DaoWalletTypeEnum.SYSTEM, request.getRemark(), request.getAccountType());
        } else {
            daoWalletService.subBalance(userCreatorInfo.getId(), null, request.getAmount(), BigDecimal.ZERO, DaoWalletTypeEnum.SYSTEM, request.getRemark(), request.getAccountType());
        }
        return Result.success();
    }


}
